package com.example.day1;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class LoginActivity extends BaseActivity {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_login);
        String nickname = getIntent().getStringExtra("nickname");
        int age = getIntent().getIntExtra("age", 0);
        Button login = findViewById(R.id.login);
        //login.setTextSize();
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                login();
            }
        });
    }

    private void login(){

        EditText et_account = findViewById(R.id.account);
        EditText et_password = findViewById(R.id.password);
        String account = et_account.getText().toString();
        String password = et_password.getText().toString();

        /**
         * {
         * "tenantEname":"giantgev",
         * "name":"admin",
         * "password":"Gev@123",
         * "hash":"qaz"
         * }
         */

        Map<String, Object> param = new HashMap<>();
        param.put("tenantEname", "giantgev");
        param.put("password", password);
        param.put("hash","qaz");
        param.put("name", account);
        apiServer.login(param)
                .subscribeOn(Schedulers.io())
                .flatMap(new Function<ResponseInfo<String>, ObservableSource<ResponseInfo<List<User>>>>() {
                    @Override
                    public ObservableSource<ResponseInfo<List<User>>> apply(ResponseInfo<String> stringResponseInfo) throws Exception {
                        String token = stringResponseInfo.getData();
                        return apiServer.getAgenList(token, 1, 10);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseInfo<List<User>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ResponseInfo<List<User>> listResponseInfo) {
                        try {
                            List<User> users = listResponseInfo.getData();
                            User user = users.get(0);
                            Log.i("TAG", user.getDeviceList().get(0).getModelName());

                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {

                    }
                });


    }

    /**
     *
     */
    @Override
    protected void onResume() {
        super.onResume();
    }

    /**
     * 界面暂停
     */
    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        setResult(-1);
        finish();
    }
}
